<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>js原理理解 | jiaojiao&#39;s blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/jiaojiao-blog/狗狗.png">
    <meta name="description" content="jiaojiao">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css" as="style"><link rel="preload" href="/jiaojiao-blog/assets/js/app.9b9a9edd.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/3.13686b80.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/1.7e378398.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/13.9bec8c42.js" as="script"><link rel="prefetch" href="/jiaojiao-blog/assets/js/10.3a421832.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/11.c69afd6f.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/12.5a57ed37.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/14.506f6e6c.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/15.d294a059.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/16.6c9ad749.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/17.fed338db.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/18.7c6cff98.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/19.1b119cf7.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/20.b4e422a5.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/21.d3188793.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/22.f0f65d80.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/23.a86a53ea.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/24.b86aa33d.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/4.cd307e36.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/5.336e8cbd.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/6.8f2d8ec2.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/7.952972ee.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/8.7270e456.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/9.6322dd00.js">
    <link rel="stylesheet" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-5bb33761><div data-v-5bb33761><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-5bb33761 data-v-5bb33761><h3 class="title" data-v-59e6cb88>jiaojiao's blog</h3> <p class="description" data-v-59e6cb88>jiaojiao</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-5bb33761><header class="navbar" data-v-5bb33761><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/jiaojiao-blog/" class="home-link router-link-active"><img src="/jiaojiao-blog/香蕉.png" alt="jiaojiao's blog" class="logo"> <span class="site-name">jiaojiao's blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-5bb33761></div> <aside class="sidebar" data-v-5bb33761><div class="personal-info-wrapper" data-v-1fad0c41 data-v-5bb33761><img src="/jiaojiao-blog/头像.jpg" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    jiaojiao
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>14</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>5</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-5bb33761><h3 class="title" data-v-59e6cb88>js原理理解</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-5bb33761><div data-v-5bb33761><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">js原理理解</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>jiaojiao</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>7/21/2022</span></i> <i class="iconfont reco-eye" data-v-8a445198><span id="/jiaojiao-blog/blogs/js_study/2022/js%E5%8E%9F%E7%90%86%E7%90%86%E8%A7%A3.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-8a445198><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>js</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="什么是闭包-闭包的应用有哪些地方"><a href="#什么是闭包-闭包的应用有哪些地方" class="header-anchor">#</a> 什么是闭包，闭包的应用有哪些地方</h1> <p>闭包就是能够读取其他函数内部变量的函数。例如在JavaScript中，只有函数内部的子函数才能读取局部变量，所以闭包可以理解成“定义在一个函数内部的函数“。在本质上，闭包是将函数内部和函数外部连接起来的桥梁。</p> <p>我知道的闭包的应用: 封装私有变量和处理回调函数</p> <ol><li>封装私有变量</li></ol> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Ninja</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 私有变量</span>
  <span class="token keyword">let</span> feints <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

  <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">getFeints</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> feints<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">feint</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    feints<span class="token operator">++</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> ninja1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Ninja</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> ninja2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Ninja</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ninja1 和 ninja2 有自己的词法环境</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;Ninja&quot;</span><span class="token punctuation">,</span> Ninja<span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ninja1 not access feints&quot;</span><span class="token punctuation">,</span> ninja1<span class="token punctuation">.</span>feints <span class="token operator">===</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;get feints&quot;</span><span class="token punctuation">,</span> ninja1<span class="token punctuation">.</span><span class="token function">getFeints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0</span>
ninja1<span class="token punctuation">.</span><span class="token function">feint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// +1</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;get feints&quot;</span><span class="token punctuation">,</span> ninja1<span class="token punctuation">.</span><span class="token function">getFeints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><ol start="2"><li>处理回调函数</li></ol> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// tick 在这里被修改</span>
  <span class="token comment">// Interval 的回调函数, 通过闭包找到这里</span>
  <span class="token keyword">let</span> tick <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;tick init&quot;</span><span class="token punctuation">,</span> tick<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">const</span> timer <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>tick <span class="token operator">&lt;</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      tick <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;tick change&quot;</span><span class="token punctuation">,</span> tick<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">clearInterval</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;tick equal 100&quot;</span><span class="token punctuation">,</span> tick <span class="token operator">===</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;access timer by closure&quot;</span><span class="token punctuation">,</span> timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h1 id="js对象-define-property方法简介"><a href="#js对象-define-property方法简介" class="header-anchor">#</a> JS对象——define Property方法简介</h1> <p>语法：Object.defineProperty(obj,property,descriptor)</p> <p>参数一：obj
绑定属性的目标对象
参数二：property
绑定的属性名
参数三：descriptor
属性描述（配置），且此参数本身为一个对象</p> <p>属性值1：value
设置属性默认值
属性值2：writable
设置属性是否能够修改
属性值3：enumerable
设置属性是否可以枚举，即是否允许遍历
属性值4：configurable
设置属性是否可以删除或编辑
属性值5：get
获取属性的值
属性值6：set
设置属性的值</p> <h3 id="功能一-通过value设置属性值"><a href="#功能一-通过value设置属性值" class="header-anchor">#</a> 功能一：通过value设置属性值</h3> <p>由于Object.defineProperty可以给对象定义属性，我们通过value，可以动态地将obj对象的属性动态地添加给target中。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>	   <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
			Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
				<span class="token literal-property property">value</span><span class="token operator">:</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
			<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>输出结果：</strong>
成功获取到了obj中的属性值</p> <h3 id="功能二-通过writable设置是否可写-修改"><a href="#功能二-通过writable设置是否可写-修改" class="header-anchor">#</a> 功能二：通过writable设置是否可写/修改</h3> <p><strong>1.当writable的值为false时</strong></p> <p>尝试将count的值从0改为10</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
			Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
				<span class="token literal-property property">value</span><span class="token operator">:</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span>
				<span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span>
			<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		target<span class="token punctuation">.</span>count <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token comment">//将count中的值0修改成10</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>target<span class="token punctuation">.</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>当writable为false时，无法改变count的值</p> <p>【注意】writable默认值为false，即不加writable方法，target的值也不可写/修改</p> <h3 id="功能三-通过enumerable设置属性值是否允许遍历"><a href="#功能三-通过enumerable设置属性值是否允许遍历" class="header-anchor">#</a> 功能三：通过enumerable设置属性值是否允许遍历</h3> <p>在上面的代码基础上，我们添加enumerable属性并尝试遍历target中的属性值</p> <p><strong>1.当enumerable的值为false时</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>	   <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
			Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
				<span class="token literal-property property">value</span><span class="token operator">:</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span>
				<span class="token literal-property property">writable</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
				<span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span>
			<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span>key <span class="token keyword">in</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
				console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//遍历target中的属性值并打印，此时结果为空</span>
		<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>输出结果：</strong>
由于此方法为fasle时，属性值无法遍历，所以无法打印</p> <p>【注意】enumerable默认值为false，即不加enumerable方法，target的值也不可写/修改</p> <h3 id="功能四-通过configurable设置属性是否可删"><a href="#功能四-通过configurable设置属性是否可删" class="header-anchor">#</a> 功能四：通过configurable设置属性是否可删</h3> <p>在上面的代码基础上，我们添加configurable属性并尝试用delete方法删除target中的属性</p> <p><strong>1.当configurable的值为false时</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>	   <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
			Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
				<span class="token literal-property property">value</span><span class="token operator">:</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span>
				<span class="token literal-property property">writable</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
				<span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span>，
				<span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span>
			<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">delete</span> target<span class="token punctuation">.</span>count<span class="token punctuation">;</span><span class="token comment">//删除count属性</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>输出结果：</strong>
由于此方法为false时，属性值无法删除，target中仍有count属性</p> <h3 id="功能五-通过get方法获取属性的值"><a href="#功能五-通过get方法获取属性的值" class="header-anchor">#</a> 功能五：通过get方法获取属性的值</h3> <p><strong>【注意!】当设置get方法时，不能有value和writable方法，否则会报错</strong>
get方法的值是一个函数，此函数不需要参数</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>		   <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
				Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
					<span class="token comment">//value: obj[key],</span>
					<span class="token comment">//writable:true,</span>
					<span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
					<span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
					<span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
						<span class="token keyword">return</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
					<span class="token punctuation">}</span>
				<span class="token punctuation">}</span><span class="token punctuation">)</span>
			<span class="token punctuation">}</span>
			console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>target<span class="token punctuation">.</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>输出结果：</strong>
成功获取到了指定属性（count）的值</p> <p>【注意】get方法只有在获取指定属性值的时候，才会触发，即调用get下的匿名函数</p> <h3 id="功能六-通过set方法设置属性的值"><a href="#功能六-通过set方法设置属性的值" class="header-anchor">#</a> 功能六：通过set方法设置属性的值</h3> <p>set方法的值也是一个函数，定义时会自动注入一个参数，此参数会设置属性的值</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>		   <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
				Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
					<span class="token comment">//value: obj[key],</span>
					<span class="token comment">//writable:true,</span>
					<span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
					<span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
					<span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
						<span class="token keyword">return</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
					<span class="token punctuation">}</span>，
					<span class="token function-variable function">set</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">val</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
						console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打印设置好的值</span>
					<span class="token punctuation">}</span>
				<span class="token punctuation">}</span><span class="token punctuation">)</span>
			<span class="token punctuation">}</span>
			target<span class="token punctuation">.</span>count <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token comment">//修改count属性的值为10</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>输出结果：</strong>
成功修改了指定属性（count）的值</p> <p>【注意】set方法只有在设置/修改指定属性值的时候，才会触发，即调用set下的匿名函数</p> <p>target与obj的比较：
target与obj都是对象，且通过defineProperty方法处理后，二者的值动态相等。
即增加、修改或删除其中任意对象的属性，另一对象也会相应地进行变化。 类似与defineProperty方法将二者以某种方式连在了一起。
target === obj 的结果为false,即二者并非同一个对象</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">7/21/2022, 8:12:10 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-ac050c62></ul></main></div> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/jiaojiao-blog/assets/js/app.9b9a9edd.js" defer></script><script src="/jiaojiao-blog/assets/js/3.13686b80.js" defer></script><script src="/jiaojiao-blog/assets/js/1.7e378398.js" defer></script><script src="/jiaojiao-blog/assets/js/13.9bec8c42.js" defer></script>
  </body>
</html>
